package com.joolun.mall.util.excel;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Font;

public class CustomHeaderStyleHandler implements CellWriteHandler {
    private CellStyle headerCellStyle;

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        if (isHead) {
            if (headerCellStyle == null) {
                // 初始化样式
                Workbook workbook = cell.getSheet().getWorkbook();
                headerCellStyle = workbook.createCellStyle();

                // 设置字体
                Font font = workbook.createFont();
                font.setBold(true);
                font.setColor(IndexedColors.BRIGHT_GREEN1.getIndex());
                font.setFontHeightInPoints((short) 12); // 字号
                font.setFontName("宋体");
                headerCellStyle.setFont(font);
                
                // 设置背景色
                headerCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
                headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

                
                // 设置居中对齐
                headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
            }
            cell.setCellStyle(headerCellStyle);
        }
    }

    // 其他方法留空
    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
    }
}